package com.bosssoft.mapper;

import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bosssoft.entity.dto.FibAccountDTO;
import com.bosssoft.entity.po.FibAccountPO;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 账户表(FibAccount)表数据库访问层
 *
 * @author makejava
 * @since 2021-08-17 15:08:02
 */
@Mapper
public interface FibAccountMapper extends BaseMapper<FibAccountPO> {

    @Select("SELECT acc_balance \n " +
            "FROM fib_account \n" +
            "WHERE deleted = 1 \n" +
            "AND acc_id = #{id} ")
    BigDecimal getBalanceById(Long id);

    @Select("SELECT acc_balance \n" +
            "FROM fib_account \n" +
            "WHERE deleted = 1 \n" +
            "AND acc_username = #{acc} ")
    BigDecimal getBalanceByAcc(Long acc);

    /**
     * 转账 入账
     * @param toId
     * @param amount
     * @return
     */
    @Update("UPDATE fib_account \n" +
            "SET acc_balance = acc_balance + #{amount} \n " +
            "WHERE acc_id = #{toId}")
    int transferInById(Long toId, BigDecimal amount);

    /**
     * 转账 出账
     * @param fromId
     * @param amount
     * @return
     */
    @Update("UPDATE fib_account \n" +
            "SET acc_balance = acc_balance - #{amount} \n " +
            "WHERE acc_id = #{fromId}")
    int transferOutById(Long fromId, BigDecimal amount);


    /**
     * 转账 入账
     * @param toAcc
     * @param amount
     * @return
     */
    @Update("UPDATE fib_account \n" +
            "SET acc_balance = acc_balance + #{amount} \n " +
            "WHERE acc_username = #{toAcc}")
    int transferInByAcc(Long toAcc, BigDecimal amount);

    /**
     * 转账 出账
     * @param fromAcc
     * @param amount
     * @return
     */
    @Update("UPDATE fib_account \n" +
            "SET acc_balance = acc_balance - #{amount} \n " +
            "WHERE acc_username = #{fromAcc}")
    int transferOutByAcc(Long fromAcc, BigDecimal amount);

    /**
     * 根据id查询
     * */
    @Select("select acc_id as accId,acc_username as accUsername,acc_balance as accBalance,deleted as deleted,acc_type as accType,acc_bank as accBank,create_time as createTime,update_time as updateTime,create_by as createBy,update_by as updateBy,version as version from fib_account where acc_id = #{accId}")
    FibAccountPO select(Long accId);
    @Select("select acc_balance as accBalance from fib_account where acc_id = #{accId}")
    int selectBalance(FibAccountPO fibAccountPO);
    @Select("select acc_id as accId,acc_username as accUsername,acc_balance as accBalance,deleted as deleted,acc_type as accType,acc_bank as accBank,create_time as createTime,update_time as updateTime,create_by as createBy,update_by as updateBy,version as version " +
            "from fib_account limit #{offset},#{limit}")
    List<FibAccountPO> selectAll(Long offset,Long limit);
    @Delete("delete from fib_account where acc_id = #{accId}")
    int delete(Long accId);
    /**
     * 修改数据
     *
     * @return 影响行数
     */
    @Update("update fib_account set \n" +
            "acc_username = #{accUsername},acc_password = #{accPassword},deleted = #{deleted},acc_type = #{accType},acc_bank = #{accBank},update_time = now(),update_by = #{updateBy},version = version + 1 \n" +
            "where acc_id = #{accId}")
    boolean update(FibAccountPO fibAccountPO);
    @Update("update fib_account set \n" +
            "deleted = #{deleted},update_time = now(),update_by = #{updateBy},version = version + 1 \n" +
            "where acc_id = #{accId}")
    boolean updateDeleted(FibAccountPO fibAccountPO);
    @Update("update fib_account set \n" +
            "acc_password = #{accPassword},update_time = now(),update_by = #{updateBy},version = version + 1 \n" +
            "where acc_id = #{accId}")
    boolean updatePassword(FibAccountPO fibAccountPO);

    @Update("update fib_account set \n" +
            "acc_balance = acc_balance + #{balance},update_time = now(),update_by = #{updateBy},version = version + 1 \n" +
            "where acc_id = #{accId} and acc_password = #{accPassword} and deleted = 1")
    boolean updateBalance(FibAccountPO fibAccountPO);
}

